package com.sywl.glzh.domain;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OrderBy;
import javax.persistence.PersistenceContext;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;
import javax.persistence.Version;
import javax.validation.constraints.Size;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Configurable
@Entity
@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class News {

    private String name;
    
    private String enName;

    @Size(max = 50000)
    private String content;
    
    @Size(max = 50000)
    private String enContent;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date updateTime;
    
    public List<News> findNewsByContent(String content) {
    	TypedQuery<News> query = entityManager.createQuery("from News where name like :type or content like :content", News.class);
    	query.setParameter("type", "%" + content + "%");
    	query.setParameter("content", "%" + content + "%");
    	return query.getResultList();
    }
    
    @OrderBy("id desc")
    public static List<News> fineSortEntities(int first, int maxResult) {
    	return findNewsEntries(first, maxResult);
    }

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

	@Version
    @Column(name = "version")
    private Integer version;

	public Long getId() {
        return this.id;
    }

	public void setId(Long id) {
        this.id = id;
    }

	public Integer getVersion() {
        return this.version;
    }

	public void setVersion(Integer version) {
        this.version = version;
    }

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	public String getName() {
        return this.name;
    }

	public void setName(String name) {
        this.name = name;
    }

	public String getEnName() {
        return this.enName;
    }

	public void setEnName(String enName) {
        this.enName = enName;
    }

	public String getContent() {
        return this.content;
    }

	public void setContent(String content) {
        this.content = content;
    }

	public String getEnContent() {
        return this.enContent;
    }

	public void setEnContent(String enContent) {
        this.enContent = enContent;
    }

	public Date getUpdateTime() {
        return this.updateTime;
    }

	public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

	@PersistenceContext
    transient EntityManager entityManager;

	public static final EntityManager entityManager() {
        EntityManager em = new News().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countNews() {
        return entityManager().createQuery("SELECT COUNT(o) FROM News o", Long.class).getSingleResult();
    }

	public static List<News> findAllNews() {
        return entityManager().createQuery("SELECT o FROM News o", News.class).getResultList();
    }

	public static News findNews(Long id) {
        if (id == null) return null;
        return entityManager().find(News.class, id);
    }

	public static List<News> findNewsEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM News o", News.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            News attached = News.findNews(this.id);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public News merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        News merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }
}
